const store = new Map();
const methods = {
	count: 0,
	async getCellsModel(a, b, c) {
		console.log("worker的getCellsModel方法被调用，参数为", a, b, c);
		const result = await get("http://localhost:3000/getData");
		console.log(result.data);
		return result.data;
	},
	storeBaseCellsModel(cm) {
		store.set("baseCM", cm);
	},
	batchCopyCellsModel(count) {
		for (let i = 0; i < count; i++) {
			store.set(`${Date.now()}${i}`, structuredClone(store.get("baseCM")));
		}
		console.log(store);
	},
	
};
onmessage = async function(ev) {
	const {functionName, args} = ev.data;
	console.log("worker被调用次数：", ++methods.count, "调用方法：", functionName, "调用参数：", args);
	if (methods[functionName]) {
		const result = methods[functionName](...args);
		if (result instanceof Promise) {
			result.then(res => {
				postMessage({
					functionName,
					result: res
				})		
			})
		} else {
			postMessage({
				functionName,
				result: result
			})
		}
	}
}

async function get(url) {
	const result = await fetch(url);
	const data = await result.json();
	console.log("worker get请求结果：", result, data);
	return data;
}
